//
//  CurrencyConversionViewController.m
//  CenturyInterconnection
//
//  Created by Tian Wei You on 16/5/16.
//  Copyright © 2016年 Shanghai DuRui Information Technology Company. All rights reserved.
//

#import "CurrencyConversionViewController.h"

@interface CurrencyConversionViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSArray *titleArray;
@property(nonatomic,strong)NSDictionary *keyDic;

@end

@implementation CurrencyConversionViewController

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(ACTUAL_HEIGHT(0),ACTUAL_HEIGHT(0), KScreenWidth, KScreenHeight) style:UITableViewStyleGrouped];
        _tableView.delegate=self;
        _tableView.dataSource=self;
    }
    return _tableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self makeNaviButton];
    [self getDatas];
    [self.view addSubview:self.tableView];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"CurrencyConversionPage"];//("PageOne"为页面名称，可自定义)
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"CurrencyConversionPage"];
}


-(void)getDatas{
    // http://viprunningonline.com/api/index.php? main_page=currencies
    
    NSString*urlStrr=[NSString stringWithFormat:@"%@main_page=currencies",HTTP_ADDRESS];
    NSDictionary*paramss=@{@"zenid":[UserSession instance].cookie};
    HttpManager*managerr=[[HttpManager alloc]init];
  
    [managerr postDataFromNetworkWithUrl:urlStrr parameters:paramss compliation:^(id data, NSError *error) {
        
        NSString*number=[NSString stringWithFormat:@"%@",data[@"status"]];
        
        if ([number isEqualToString:@"1"]) {
            self.keyDic=data[@"data"][@"currencies"];
            self.titleArray=self.keyDic.allKeys;
            [self.tableView reloadData];
        }
    }];
}

-(void)makeNaviButton{
    
    self.title=@"CURRENCY";
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"nav_back"] forState:0];
    leftButton.frame=CGRectMake(0, 0, ACTUAL_WIDTH(23), ACTUAL_HEIGHT(23));
    [leftButton addTarget:self action:@selector(comeBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*item= [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem=item;
}

#pragma mark 返回事件
-(void)comeBack{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 设置tableview
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.00001;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"666"];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"666"];
    }
    
//  cell.backgroundColor=[UIColor yellowColor];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    if (self.titleArray.count>0) {
        cell.textLabel.text=self.titleArray[indexPath.row];
        cell.textLabel.font = FONT_SYSTEM_28;
        cell.textLabel.textColor = RGBACOLOR(51, 51, 51, 1);
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // http://viprunningonline.com/api/index.php? main_page=currencies
    
    NSString*urlStrr=[NSString stringWithFormat:@"%@main_page=currencies",HTTP_ADDRESS];
    NSDictionary*paramss=@{@"currency":self.titleArray[indexPath.row],@"zenid":[UserSession instance].cookie};
    HttpManager*managerr=[[HttpManager alloc]init];
    
    [managerr postDataFromNetworkWithUrl:urlStrr parameters:paramss compliation:^(id data, NSError *error) {
        
        NSString*number=[NSString stringWithFormat:@"%@",data[@"status"]];
        
        if ([number isEqualToString:@"1"]) {
            NSDictionary *dict = @{@"index" : self.titleArray[indexPath.row]};
            [MobClick event:@"CurrencyConversionClick" attributes:dict];
            [JRToast showWithText:@"Choose success！" duration:2];
            
            [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",indexPath.row] forKey:@"MONEYINDEX"];
            [[NSUserDefaults standardUserDefaults]setObject:self.titleArray[indexPath.row] forKey:@"CURRENCY"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    }];
    
    [self comeBack];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
